package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.framework.utils.JPushHelper;
import com.baichang.taoshunda.dao.PushDao;
import com.baichang.taoshunda.entity.Bussiness;
import com.baichang.taoshunda.entity.DeliveryClerk;
import com.baichang.taoshunda.entity.Push;
import com.baichang.taoshunda.entity.User;
import com.baichang.taoshunda.service.*;
import com.baichang.taoshunda.utils.MQuantity;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 推送消息实现类
 * Created by Dylan on 2017/8/4
 */
@Service
public class PushServiceImpl implements PushService {

    @Autowired
    private PushDao pushDao;

    @Autowired
    private BusinessService businessService;
    @Autowired
    private UserService userService;
    @Autowired
    private DeliveryClerkService deliveryClerkService;
    @Autowired
    private LinkUrlService linkUrlService;


    /**
     * 查看消息详情
     */
    @Override
    public Map<String, Object> getPushDetail(Integer id) {
        Push push = pushDao.getMessageById(id);
        Map<String, Object> map = push.toMap();
        return map;
    }

    /**
     * 查询推送消息列表  用到
     */
    @Override
    public Page<Map<String, Object>> pushQuery(int nowPage, int pageSize, String tuisongren, String xjstart, String xjend, String tuisongtitle) throws ServiceException {
        return pushDao.pushQuery(nowPage, pageSize, tuisongren, xjstart, xjend, tuisongtitle);
    }

    /**
     * 删除推送消息
     */
    @Override
    public boolean deletePushById(int id) throws ServiceException {
        return pushDao.deletePushById(id);
    }

    /**
     *
     * 推送消息
     *
     * @param
     * @param title    推送标题
     * @param content  详细内容
     * @param user_ids 推送用户ids
     * @return
     */
    public boolean pushMsg(String picture, String title, String content, String user_ids, String remark, int pushType, String tags) {
        Push pm = new Push();
        pm.setTitle(title);
        pm.setPicture(picture);
        pm.setContent(content);
        if (pushType == 1) {//标签推送
            pm.setUsers(tags);
        } else if (pushType == 0) {//别名
            pm.setUsers(user_ids);
        } else if (pushType == 2) { //全部
            pm.setUsers("全部");
        }
        pm.setRemark(remark);
        pm.setCreateTime(new Date());
        Long f = pushDao.save(pm);
        return pushContent(title, remark, content, user_ids, pushType, f, tags);
    }

    /**
     * 整理需要发送的数据，并执行
     */
    public boolean pushContent(String title, String remark, String content, String user_ids, int pushType, Long f, String tags) {
        //封装提示的url数据
        JSONObject jsonObject = new JSONObject();
        Map<String, Object> map = new HashMap<String, Object>();
        Push p = pushDao.findAllPushMsg();
        map.put("msgid", (p.getId()));
        map.put("title", title);
        map.put("remark", remark);
        map.put("pushType", "message");
        map.put("link", MQuantity.pushLink + p.getId());
        String data = JSONObject.fromObject(map).toString();

        if (pushType == 0) {//别名
            String[] users = user_ids.split(",");
            List ulist = Arrays.asList(users);
            JPushHelper jPushHelper = new JPushHelper("user.properties");
            JPushHelper jPushHelper1 = new JPushHelper("business.properties");
            JPushHelper jPushHelper2 = new JPushHelper("distribution.properties");
            jPushHelper.sendByAlias(title, remark, data, ulist);
            jPushHelper1.sendByAlias(title, remark, data, ulist);
            jPushHelper2.sendByAlias(title, remark, data, ulist);

//            JPushUtils.sendByAlias(title,remark,data,ulist);
        } else if (pushType == 2) {//全部
            JPushHelper jPushHelper = new JPushHelper("user.properties");
            JPushHelper jPushHelper1 = new JPushHelper("business.properties");
            JPushHelper jPushHelper2 = new JPushHelper("distribution.properties");
            jPushHelper.sendAll(title, remark, data);
            jPushHelper1.sendAll(title, remark, data);
            jPushHelper2.sendAll(title, remark, data);
        } else if (pushType == 1) {//标签推送
            String[] tag = tags.split(",");
            for (int i = 0; i < tag.length; i++) {
                String singleData = tag[i];
                if (singleData.equals("用户端")) {
                    JPushHelper jPushHelper = new JPushHelper("user.properties");
                    List<String> tagList = new ArrayList<>();
                    tagList.add("user");
                    jPushHelper.sendByTags(title, remark, data, tagList);
                } else if (singleData.equals("商家端")) {
                    JPushHelper jPushHelper1 = new JPushHelper("business.properties");
                    List<String> tagList = new ArrayList<>();
                    tagList.add("business");
                    jPushHelper1.sendByTags(title, remark, data, tagList);
                } else if(singleData.equals("配送端")){
                    JPushHelper jPushHelper2 = new JPushHelper("distribution.properties");
                    List<String> tagList = new ArrayList<>();
                    tagList.add("distribution");
                    jPushHelper2.sendByTags(title, remark, data, tagList);
                }
            }
        }
        return true;
    }

    /**
     * 删除消息
     */
    public boolean delete(int id) throws ServiceException {
        if (id == 0) {
            throw new ServiceException("编号错误");
        }
        if (pushDao.delete(id) > 0) {
            return true;
        }
        return false;
    }

    /**
     * 查看商家的消息列表
     *
     * @param bussId   商家编号
     * @param nowPage  当前页
     * @param pageSize 每页大小
     * @return
     */
    @Override
    public List<Map<String, Object>> findBusinessPushList(Integer bussId, Integer nowPage, Integer pageSize) throws ServiceException {
        Bussiness bussiness = businessService.getBusinessById(bussId);
        return pushDao.findBusinessPushList(bussiness.getAccount(), nowPage, pageSize);
    }

    @Override
    public List<Map<String, Object>> findUserPushList(Integer userId, Integer nowPage, Integer pageSize) throws ServiceException {
        User user = userService.getUserBeanById(userId);
        if (user == null) {
            throw new ServiceException("该用户不存在");
        }
        List<Map<String, Object>> maplist = pushDao.findUserPushList(user.getAccount(), nowPage, pageSize, userId);
//            for (int i=0;i<maplist.size();i++){
//                Map<String, Object> linkUrl = linkUrlService.getLinkUrlById(4);
//                String url = linkUrl.get("link").toString() + maplist.get(i).get("id");
//                maplist.get(i).put("linkUrl",url);
//            }
        return maplist;
    }

    /**
     * 接口查看配送员的消息列表
     *
     * @param disId    配送员编号
     * @param nowPage  当前页
     * @param pageSize 每页大小
     * @return
     */
    @Override
    public List<Map<String, Object>> findPushList(Integer disId, Integer nowPage, Integer pageSize) throws ServiceException {
        DeliveryClerk deliveryClerk = deliveryClerkService.getDeliveryClerkById(disId);
        return pushDao.findPushList(deliveryClerk.getAccount(), nowPage, pageSize);
    }
}
